﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace lcs.Web.demandplaninfo
{
    public partial class Add : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds_user = Session["login"] as DataSet;

                    string user_depid = ds_user.Tables[0].Rows[0]["user_depid"].ToString();

                    lcs.BLL.pmtypeinfo pmbll = new BLL.pmtypeinfo();
                    DataSet ds = pmbll.GetList("");
                    ddlptype.DataSource = ds;
                    ddlptype.DataTextField = "pmtype";
                    ddlptype.DataValueField = "pmid";
                    ddlptype.DataBind();

                    string pmid = this.ddlptype.SelectedValue;

                    BindDDL_FLOW(user_depid, pmid);

                    Page.DataBind();

                }
                
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (IsLogin())
            {
                string strErr = "";
                string proid = hfpid.Value;
                string flowSelect = this.ddl_flow.SelectedValue;
                if (this.txtpbh.Text.Trim().Length == 0)
                {
                    strErr += "计划编号不能为空！\\n";
                }
                if (proid == "")
                {
                    strErr += "项目不能为空！\\n";

                }
                if (flowSelect == "")
                {
                    strErr += "审批流程不能为空！\\n";
                }
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }
                
                string pbh = this.txtpbh.Text;
                int uid = getid();
                string dep = getdep();
                int ptype = int.Parse(this.ddlptype.SelectedValue.ToString());
                
                lcs.Model.demandplaninfo model = new lcs.Model.demandplaninfo();
                model.pbh = pbh;
                model.uid = uid;
                model.ptype = ptype;
                model.proid = proid;
                if (pxy.Visible == true)
                {
                    int mtid = int.Parse(this.ddlm.SelectedValue.ToString());
                    int sid = int.Parse(this.ddls.SelectedValue.ToString());
                    model.mtid = mtid;
                    model.sid = sid;

                }
                
                lcs.BLL.demandplaninfo bll = new lcs.BLL.demandplaninfo();
                int documentId = bll.Add(model);

                Maticsoft.BLL.documentFlowMapping dmb = new Maticsoft.BLL.documentFlowMapping();

                string documentFlowMapping_name = this.ddl_flow.SelectedValue;
                DataSet ds_dm = dmb.GetList(" documentFlowMapping_name='" + documentFlowMapping_name + "' order by flow_order  ");

                Maticsoft.BLL.documentFlowInfo dfb = new Maticsoft.BLL.documentFlowInfo();
                Maticsoft.Model.documentFlowInfo df = new Maticsoft.Model.documentFlowInfo();

                if (ds_dm.Tables[0].Rows.Count > 0)
                {
                    df.flow_id = Convert.ToInt32(ds_dm.Tables[0].Rows[0]["flow_id"].ToString());
                    df.flow_name = ds_dm.Tables[0].Rows[0]["flow_name"].ToString();
                    df.document_id = documentId;

                    DataSet ds_flowNode = new Maticsoft.BLL.flow_node().GetList(" flow_id=" + df.flow_id + " and flow_node_num = 1");

                    df.flow_node_num_now = 1;
                   
                    if (ds_flowNode.Tables[0].Rows.Count == 1)
                    {
                        df.flow_node_id_now = Convert.ToInt32(ds_flowNode.Tables[0].Rows[0]["flow_node_id"]);
                    }                 
                    df.isDone = false;
                    df.status = 0;

                    foreach (DataRow row in ds_dm.Tables[0].Rows)
                    {
                        df.flow_ids += "|" + row["flow_id"].ToString();//将此公文的所有经过流程记录
                    }
                    df.documentFlowMapping_name = documentFlowMapping_name;

                    dfb.Add(df);
                }
               

                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "List.aspx");
            }
            else
            {
                Response.Redirect("~/jump.html");
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx");
        }

      

        //采购类别下拉框 
        protected void ddlptype_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (Session["login"] != null)
            {
                DataSet ds_user = Session["login"] as DataSet;

                string user_depid = ds_user.Tables[0].Rows[0]["user_depid"].ToString();

                string pmid = this.ddlptype.SelectedValue;
                string pname = this.ddlptype.SelectedItem.Text;
                if (pname.Contains("协议"))
                {
                    pxy.Visible = true;
                    BLL.protocolquotainfo pbll = new BLL.protocolquotainfo();
                    int depid = getdepid();
                    DataSet dsm = pbll.GetMdll(depid);
                    ddlm.DataSource = dsm;
                    ddlm.DataTextField = "mtname";
                    ddlm.DataValueField = "mtid";
                    ddlm.DataBind();                    
                    int mtid = 0;
                    if (dsm.Tables[0].Rows.Count > 0)
                    {
                        int.TryParse(ddlm.Items[0].Value, out mtid);
                        DataSet dss = pbll.GetSdll(depid, mtid);
                        ddls.DataSource = dss;
                        ddls.DataTextField = "sname";
                        ddls.DataValueField = "sid";
                        ddls.DataBind();
                    }
                }
                else
                {
                    pxy.Visible = false;
                }
                BindDDL_FLOW(user_depid,pmid) ;
            }
        }

        private void BindDDL_FLOW(string user_depid,string pmid)
        {
            Maticsoft.BLL.doc_pmtype dcm = new Maticsoft.BLL.doc_pmtype();

            DataSet ds = dcm.GetDistinctList(" depid=" + user_depid + " and pmid= " + pmid);
            ddl_flow.DataSource = ds;
            ddl_flow.DataTextField = "documentFlowMapping_name";
            ddl_flow.DataValueField = "documentFlowMapping_name";
            ddl_flow.DataBind();

        }

        protected void ddlm_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (pxy.Visible == true)
            {
                BLL.protocolquotainfo pbll = new BLL.protocolquotainfo();
                int depid = getdepid();
                int mtid=0;
                int.TryParse(ddlm.SelectedValue.ToString(), out mtid);
                DataSet dss = pbll.GetSdll(depid,mtid);
                ddls.DataSource = dss;
                ddls.DataTextField = "sname";
                ddls.DataValueField = "sid";
                ddls.DataBind();
            }
        }
    }
}
